from appium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import time,os
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Base():

    def __init__(self):
        #获取root权限
        os.system('adb root')
        cap={
            "platformName": "Android",
            "deviceName": "emulator-5554",
            "appPackage": "com.xiachufang",
            "appActivity": "com.xiachufang.activity.home.HomeActivity",
            "noReset": True
        }
        self.driver = webdriver.Remote('http://127.0.0.1:4723/hd.hub',cap)
        self.driver.implicitly_wait(25)
        sleep(15)
        #获取界面元素方法
    def find_ele(self,loc):
        if type(loc)==str:
            return self.driver.find_element_by_android_uiautomator(loc)
        else:
            return self.driver.find_element(*loc)
        #设置隐式等待方法
    def find_wait_ele(self,loc):
        return WebDriverWait(self.driver,15).until(EC.visibility_of_element_located(loc))
        #查找页面元素
    def get_key_value(self,loc,key):
        ele= self.driver.find_element(loc)
        ele.get_attribute(key)
        #轻敲
    def qingqiao(self,x1,y1):
        self.driver.tap([(x1,y1)],500)
        #滑动
    def huadong(self,x1,y1,x2,y2):
        self.driver.swipe(x1,y1,x2,y2,500)
        #截屏
    def jieping(self,filename):
        t=time.strftime('%Y%m%d%H%M%S',time.localtime())
        self.driver.save_screenshot(t+filename)
        #退出
    def tuichu(self):
        self.driver.quit()